//给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写。 
//
// 说明：本题中，我们将空字符串定义为有效的回文串。 
//
// 
//
// 示例 1: 
//
// 
//输入: "A man, a plan, a canal: Panama"
//输出: true
//解释："amanaplanacanalpanama" 是回文串
// 
//
// 示例 2: 
//
// 
//输入: "race a car"
//输出: false
//解释："raceacar" 不是回文串
// 
//
// 
//
// 提示： 
//
// 
// 1 <= s.length <= 2 * 10⁵ 
// 字符串 s 由 ASCII 字符组成 
// 
// Related Topics 双指针 字符串 👍 531 👎 0


import java.util.ArrayList;
import java.util.List;

/**
 * [0125]验证回文串-ValidPalindrome
 * <p>
 * 算法: 首尾校验
 * <br>
 * 时间复杂度: O(n)
 * <br>
 * 空间复杂度: O(1)
 * <p>
 * 知识点:
 * <br>
 * 1. ASCII 字符转换, 0-48/A-65/a-97, 大小写之间相差32                <br>
 * 2. Character.isLetterOrDigit(c);Character.toLowerCase(c)       <br>
 * 3. ArrayList 的性能比 StringBuilder 好很多                        <br>
 * 4. 有很多过程需要开辟额外空间的, 但结果不需要额外空间的, 可以考虑原地算法
 *
 * @author yonxao
 * @since 2022-06-06 18:19:40
 */
class ValidPalindrome {
    @SuppressWarnings("all")
//leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isPalindrome(String s) {
            int left = 0, right = s.length() - 1;
            while (left < right) {
                while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
                    left++;
                }
                while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
                    right--;
                }
                if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                    return false;
                }
                left++;
                right--;
            }
            return true;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        String s1 = "0P";
        System.out.println(isPalindromeOptimize(s1));
    }

    /**
     * 算法: 首尾校验
     * <br>
     * 时间复杂度: O(n)
     * <br>
     * 空间复杂度: O(n)
     * <p>
     * 知识点: ASCII 字符转换, 0-48/A-65/a-97, 大小写之间相差32
     * <br>
     */
    public static boolean first(String s) {
        // 只考虑字母和数字字符
        // 忽略字母的大小写
        // 空字符串定义为有效的回文串
        // 0-48、A-65、a-97

        // 遍历有效字符串
        List<Integer> ca = new ArrayList<>(); // space n
        for (int i = 0; i < s.length(); i++) { // time n
            int c = s.charAt(i);
            boolean num = c >= 48 && c <= 57;
            boolean upper = c >= 65 && c <= 90;
            boolean lower = c >= 97 && c <= 122;
            if (upper) {
                c += 32;
            }
            if (num || upper || lower) {
                ca.add(c);
            }
        }

        System.out.println(ca);

        int length = ca.size();
        for (int i = 0; i < length / 2; i++) { // time n/2
            if (ca.get(i) != ca.get(length - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 算法: 首尾校验
     * <br>
     * 时间复杂度: O(n)
     * <br>
     * 空间复杂度: O(n)
     * <p>
     * 知识点: Character.isLetterOrDigit(c);Character.toLowerCase(c)
     * <br>
     */
    public static boolean firstOptimize(String s) {

        StringBuilder sb = new StringBuilder(); // space n
        for (int i = 0; i < s.length(); i++) { // time n
            char c = s.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                if (Character.isUpperCase(c)) {
                    c = Character.toLowerCase(c);
                }
                sb.append(c);
            }
        }

        System.out.println(sb);

        int length = sb.length();
        for (int i = 0; i < length / 2; i++) { // time n/2
            if (sb.charAt(i) != sb.charAt(length - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 算法: 首尾校验
     * <br>
     * 时间复杂度: O(n)
     * <br>
     * 空间复杂度: O(n)
     * <p>
     * 知识点: ArrayList 的性能比 StringBuilder 好很多
     * <br>
     */
    public static boolean firstOptimize2(String s) {

        List<Integer> ca = new ArrayList<>(); // space n
        for (int i = 0; i < s.length(); i++) { // time n
            int c = s.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                if (Character.isUpperCase(c)) {
                    c = Character.toLowerCase(c);
                }
                ca.add(c);
            }
        }

        System.out.println(ca);

        int length = ca.size();
        for (int i = 0; i < length / 2; i++) { // time n/2
            if (ca.get(i) != ca.get(length - 1 - i)) {
                return false;
            }
        }
        return true;
    }


    /**
     * 算法: 原地双指针
     * <br>
     * 时间复杂度: O(n)
     * <br>
     * 空间复杂度: O(1)
     * <p>
     * 知识点:
     * <br>
     */
    public static boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while (left < right) {
            while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
                left++;
            }
            while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
                right--;
            }
            if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }


    public static boolean isPalindromeOptimize(String s) {
        int left = 0, right = s.length() - 1;
        for (; left < right; left++, right--) {
            while (left < right && !isLetterOrDigit(s.charAt(left))) {
                left++;
            }
            while (left < right && !isLetterOrDigit(s.charAt(right))) {
                right--;
            }

            int lc = s.charAt(left), rc = s.charAt(right);
            if (isUpperLetter(lc)) {
                lc += 32;
            }
            if (isUpperLetter(rc)) {
                rc += 32;
            }

            if (lc != rc) {
                return false;
            }
        }
        return true;
    }

    public static boolean isLetterOrDigit(char c) {
        boolean num = c >= 48 && c <= 57;
        boolean upper = c >= 65 && c <= 90;
        boolean lower = c >= 97 && c <= 122;
        return num || upper || lower;
    }

    public static boolean isNotLetterOrDigit(char c) {
        boolean num = c >= 48 && c <= 57;
        boolean upper = c >= 65 && c <= 90;
        boolean lower = c >= 97 && c <= 122;
        return !num && !upper && !lower;
    }

    public static boolean isUpperLetter(int c) {
        return c >= 65 && c <= 90;
    }


}